package com.andooco.dataq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.andooco.dataq.service.jdbc.JdbcService;
import com.andooco.dataq.service.jdbc.dto.JdbcDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 数据库增删操作管理
 *
 * @author andooco
 * 20201125
 */
@Slf4j
@Controller
@RequestMapping("/jdbc")
public class JdbcController {
    @Autowired
    private JdbcService jdbcService;

    /**
     * 保存数据源信息 andooco 20201123
     *
     * @param form
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save(Model model, JdbcDto form) throws Exception {
        log.info("进入方法JdbcController.save() {}", JSON.toJSONString(form));
        String result = jdbcService.saveJdbcList(form);
        log.info("保存数据库结束：{}", result);
        return result;
    }

    /**
     * 数据库存量数据展示
     * andooco 20201125
     *
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam Map<String, Object> params) throws Exception {
        //查询数据库别名
        String jdbcName = (String) params.get("jdbcName");
        log.error("查询条件数据库别名：{}", jdbcName);
        List<JdbcDto> temps = jdbcService.findJdbcList(jdbcName);
        log.info("查询结果为：{}", JSON.toJSONString(temps));
        JSONObject result = new JSONObject();
        result.put("jdbcName", jdbcName);
        result.put("data", JSON.parseArray(JSON.toJSONString(temps)));
        return result;
    }

    /**
     * 删除数据源信息 andooco 20201126
     *
     * @param uid
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    public String del(Model model, String uid) throws Exception {
        log.info("进入方法JdbcController.del() {}", uid);
        String result = jdbcService.delJdbcList(uid);
        log.info("保存数据库结束：{}", result);
        return result;
    }

    /**
     * 测试数据库连接 andooco 20201126
     *
     * @param form
     * @return
     * @throws Exception
     */
    @RequestMapping("/testConnection")
    @ResponseBody
    public String testConnection(Model model, JdbcDto form) throws Exception {
        log.info("进入方法JdbcController.testConnection() {}", JSON.toJSONString(form));
        String result = jdbcService.testConnection(form);
        log.info("测试数据库连接结束：{}", result);
        return result;
    }

    /**
     * 测试本行数据库连接 andooco 20201126
     *
     * @param uid
     * @return
     * @throws Exception
     */
    @RequestMapping("/testConnectionThis")
    @ResponseBody
    public String testConnectionThis(Model model, String uid) throws Exception {
        log.info("进入方法JdbcController.testConnectionThis() {}", uid);
        String result = jdbcService.testConnectionThis(uid);
        log.info("测试本行数据库连接结束：{}", result);
        return result;
    }
}
